//
//  DateTimeView.m
//  CalCounter
//
//  Created by Le thanh Lam on 06/09/2013.
//  Copyright (c) 2013 Le thanh Lam. All rights reserved.
//

#import "DateTimeView.h"

@interface DateTimeView (){
    
}

@end

@implementation DateTimeView
@synthesize delegate;

static NSDate *selectDate;

+ (NSDate *)selectDate{
    if(selectDate == nil){
        selectDate = [NSDate date];
    }
    return selectDate;
}
- (void)awakeFromNib{
//    date = [NSDate date];
//    [self setTimeForLabel:date];
//    UIButton *btnNavigationTime = [UIButton buttonWithType:UIButtonTypeCustom];
//    [btnNavigationTime setFrame:CGRectMake(57, 0, 195, 44)];
//    [btnNavigationTime addTarget:self action:@selector(btnNavigationClicked:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:btnNavigationTime];
    
    [self setTimeForLabel:[DateTimeView selectDate]];
}

- (void)refresh{
    [self setTimeForLabel:[DateTimeView selectDate]];
}
- (IBAction)btnPrevClicked:(id)sender {
    [self timeNext:NO];
}

- (IBAction)btnNextClicked:(id)sender {
    [self timeNext:YES];
}

- (IBAction)btnNavigationClicked:(UIButton *)sender {
    
//    sender.selected = !sender.selected;
//    if(sender.selected){
//        _bgrImageView.image = [UIImage imageNamed:@"daynav_bg_selected.png"];
//    }else{
//        _bgrImageView.image = [UIImage imageNamed:@"daynav_bg_normal.png"];        
//    }
    
    selectDate = [NSDate date];
    [self setTimeForLabel:selectDate];
    //delegate
    if (delegate && [delegate respondsToSelector:@selector(whenChangeTime:)]) {
        [delegate whenChangeTime:selectDate];
    }
}


- (void)setTimeForLabel:(NSDate *)_dateShow{
    NSDateFormatter *formatDate = [[NSDateFormatter alloc] init];
    [formatDate setDateFormat:@"yyyy-MM-dd"];
    NSString *strDate = [formatDate stringFromDate:[NSDate date]];
    NSString *strDateShow = [formatDate stringFromDate:_dateShow];
    if ([strDate isEqualToString:strDateShow]) {
        [lblDateTime setText:NSLocalizedString(@"today", nil)];
    }
    else{
        [lblDateTime setText:strDateShow];
    }
}

- (void)timeNext:(BOOL)isNext{
    NSDateFormatter *formatDate = [[NSDateFormatter alloc] init];
    [formatDate setDateFormat:@"yyyy-MM-dd"];
    NSString *str;
    if ([lblDateTime.text isEqualToString:NSLocalizedString(@"today", nil)]) {
        str = [formatDate stringFromDate:[DateTimeView selectDate]];
    }
    else{
        str = lblDateTime.text;
    }
    NSDate *currentShowDate = [formatDate dateFromString:str];
    if (isNext) {
        currentShowDate = [NSDate dateWithTimeInterval:24*60*60 sinceDate:currentShowDate];
    }
    else{
        currentShowDate = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:currentShowDate];
    }
    [self setTimeForLabel:currentShowDate];
    
    selectDate = currentShowDate;
    
    //delegate
    if (delegate && [delegate respondsToSelector:@selector(whenChangeTime:)]) {
        [delegate whenChangeTime:currentShowDate];
    }

}

@end
